package com.rover022.flixelgame.world
{
	import com.rover022.flixelgame.GameConfig;
	import com.rover022.flixelgame.component.ai.OrdinaryFighterAI;
	import com.rover022.flixelgame.element.BaseElement;
	import com.rover022.flixelgame.manager.WorldManager;
	import com.rover022.flixelgame.ui.battle.BattleInfoUI;
	import com.rover022.flixelgame.ui.battle.BattleOverInfo;
	import com.rover022.flixelgame.world.displayobject.FlxLoadImage;
	
	import flash.display.MovieClip;
	
	import ghostcat.ui.PopupManager;
	
	import org.flixel.FlxG;
	import org.flixel.FlxGroup;
	import org.flixel.FlxTimer;

	/**
	 * 自动战斗类型的战场
	 * 类似刀塔传奇类型的游戏
	 * @author flashpig
	 *
	 */
	public class AutoFightBattleState extends BattleState
	{
		public var battleTime:Number = 90; //默认时间 30秒
		public var isSucceed:Boolean = false; //是否成功
		public var gameMode:String = GameMode.GameReady;
		private var _clockTime:FlxTimer; //计时器;
		public var enemys:Array=[];
		public var heros:Array=[];
		private var backGround:FlxLoadImage;
		private var leftLine:Array = [[308, 400], [243, 270], [177, 400], [116, 270], [52, 400]]; //阵形1 点数组 从右边第一个开始算
		private var rightLine:Array = [[714, 400], [770, 270], [845, 400], [904, 270], [970, 400]]; //阵形2点数组 从左边第一个开始算
		public var uiMovieClip:MovieClip;
		public var battleInfoUI:BattleInfoUI;
		public var defeatBackGroundID:String = "assets/UI/alpha/HVGA/battle_bg/bbg_arena.jpg"

		public function AutoFightBattleState(vars:Object = null)
		{
			initProps(vars);
		}

		/**
		 * 设置属性
		 * @param vars
		 *
		 */
		public function initProps(vars:*):void
		{
			if (vars != null)
			{
				for (var p:String in vars)
				{
					//trace(p, vars[p]);
					if (p in this)
					{
						this[p] = vars[p];
					}
				}
			}
		}

		override public function create():void
		{
			FlxG.bgColor = FlxG.BLUE;
			backGround = new FlxLoadImage();
			backGround.setImageUrl(GameConfig.webroot + defeatBackGroundID);
			add(backGround);
			playLay = new FlxGroup();
			add(playLay);
			//
			gameStart();
		}

		public function initialize():void
		{
			enemys = [];
			heros = [];
		}

		private function initUI():void
		{
			battleInfoUI = new BattleInfoUI();
			WorldManager.getInstance().showUI(battleInfoUI);
			
		}

		private function initHeroSide():void
		{
			//一般是根据xml里面的描述来产生对应的enemys
			for (var i:int = 0; i < heros.length; i++)
			{
				var _element:BaseElement = heros[i]; //根据xml描述应该有自己的ai了
				//trace(leftLine[i],leftLine[i][0]);
				_element.setPosition(leftLine[i][0], leftLine[i][1]);
				playLay.add(_element);
				//add ai system
				var ai:OrdinaryFighterAI = new OrdinaryFighterAI(_element, 0, 0);
				_element.addComponent(ai);
			}
			
		}

		private function initEnemySide():void
		{
			//一般是根据xml里面的描述来产生对应的enemys
			for (var i:int = 0; i < enemys.length; i++)
			{
				var _element:BaseElement = enemys[i]; //根据xml描述应该有自己的ai了
				_element.setPosition(rightLine[i][0], rightLine[i][1]);
				playLay.add(_element);
				//add ai system
				var ai:OrdinaryFighterAI = new OrdinaryFighterAI(_element, 0, 0);
				_element.addComponent(ai);
			}
			
		}

		/**
		 * 游戏开始初始化所有元素
		 *
		 */
		public function gameStart():void
		{
			initUI();
			initHeroSide();
			initEnemySide();
			playLay.sort("y");
			_clockTime = new FlxTimer();
			_clockTime.start(1, battleTime, countHandle);
			gameMode = GameMode.GameRun;
		}

		//修改UI界面上面的时间显示
		private function countHandle(e:FlxTimer):void
		{
			battleInfoUI.updateTime(_clockTime.loopsLeft)
			trace("timeLeft:", _clockTime.loopsLeft);
			//检查
			//gameOverCheck();
		}

		/**
		 *  游戏结束
		 *  弹出游戏的统计信息面板
		 */
		public function gameOver():void
		{
			gameMode = GameMode.GameOver;
			_clockTime.stop();
			var _info:BattleOverInfo = new BattleOverInfo();
			PopupManager.instance.showPopup(_info);
		}

		/**
		 * 释放一些类
		 */
		override public function destroy():void
		{
			if (_clockTime)
			{
				_clockTime.destroy();
			}
			uiMovieClip = null;
			battleInfoUI = null;
			super.destroy();
		}

		override public function update():void
		{
			//战场结束检测;
			super.update();
		}

		override public function gameOverCheck():void
		{
			if (gameMode == GameMode.GameRun)
			{
				if (_clockTime.loopsLeft == 0)
				{
					isSucceed = false;
					gameOver();
					return;
				}
				var alive:Boolean = false;
				for (var i:int = 0; i < enemys.length; i++)
				{
					var baseE:BaseElement = enemys[i];
					if (baseE.hp > 0)
					{
						alive = true;
						break;
					}
				}
				if (!alive)
				{
					isSucceed = true;
					gameOver();
					return;
				}
				alive = false;
				for (i = 0; i < heros.length; i++)
				{
					baseE = heros[i];
					if (baseE.hp > 0)
					{
						alive = true;
						break;
					}
				}
				if (!alive)
				{
					isSucceed = false;
					gameOver();
					return;
				}
			}
		}
	}
}
